import { Component } from '@angular/core';

import { Download } from './download';
import { DownloadService } from './download.service';

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html',
    styleUrls: [
        'app.component.css'
    ]
})
export class AppComponent {
    tmsg: string;
    ttype: string;
    dl = new Download();

    // 注入服务
    constructor(
        private dlService: DownloadService
    ) { }

    onSubmit() {
        if (!this.dl.url) {
            return this.tipWarn('下载链接不能为空');
        }
        if (!this.dl.filename) {
            return this.tipWarn('文件名不能为空');
        }
        this.tipInfo('开始下载...');

        this.dlService.downloadFile(this.dl).then(data => {
            this.tipInfo(data)
        }, err => {
            this.tipWarn(err);
        });
    }

    tipWarn(msg: string) {
        this.tmsg = msg;
        this.ttype = 'warning';
    }

    tipInfo(msg: string) {
        this.tmsg = msg;
        this.ttype = 'info';
    }

}